// const test1Path = join(__dirname, '../../resources/test1.csx').replace('app.asar', 'app.asar.unpacked')
import {join} from "path";
import edge from "electron-edge-js";

const dllPath = join(__dirname, '../../resources/OfficeScript.dll').replace('app.asar', 'app.asar.unpacked')
const dllTest = edge.func({
  assemblyFile: dllPath,
  typeName: 'Startup',
  methodName: 'DllApplication'
});

let dllApp

async function getApp() {
  if (dllApp) {
    return dllApp
  }
  return new Promise((resolve, reject) => {
    dllTest(null, (error, result) => {
      if (error) {
        return reject(error)
      }
      resolve(dllApp = result)
    })
  })
}

export async function wordToPdf(sourcePath: string, targetPath: string) {
  const app = await getApp()
  return new Promise<any>((resolve, reject) => {
    app.wordToPdf({sourcePath, targetPath}, (error, result) => {
      if (error) {
        return reject(error)
      }
      resolve(result)
    });
  })
}

export async function excelToPdf(sourcePath: string, targetPath: string) {
  const app = await getApp()
  return new Promise<any>((resolve, reject) => {
    app.excelToPdf({sourcePath, targetPath}, (error, result) => {
      if (error) {
        return reject(error)
      }
      resolve(result)
    });
  })
}

export async function pptToPdf(sourcePath: string, targetPath: string) {
  const app = await getApp()
  return new Promise<any>((resolve, reject) => {
    app.pptToPdf({sourcePath, targetPath}, (error, result) => {
      if (error) {
        return reject(error)
      }
      resolve(result)
    });
  })
}